using UnityEngine;
using System.Collections;

public class MoveObject : MonoBehaviour
{
	enum MoveType {Time, Speed}

    public void GoTo(string targetCamera)
    {
        GameObject go = GameObject.Find(targetCamera);
        if(go!=null)
            GoTo(go.transform, 2f);
    }

	public void GoTo(Transform t, float time){
        StartCoroutine(Translation(transform, transform.position, t.position, time, MoveType.Time));
        StartCoroutine(Rotation(transform, t.rotation, time));
	}

	IEnumerator Translation (Transform thisTransform, Vector3 deltaPos, float value, MoveType moveType) {
		return Translation (thisTransform, thisTransform.position, thisTransform.position + deltaPos, value, moveType);
	}

	IEnumerator Translation (Transform thisTransform, Vector3 startPos, Vector3 endPos, float value, MoveType moveType) {
		float rate = (moveType == MoveType.Time)? (float)(1.0/value) : (float)(1.0/Vector3.Distance(startPos, endPos) * value);
		float t = 0.0f;
		while (t < 1.0) {
			t += Time.deltaTime * rate;
			thisTransform.position = Vector3.Lerp(startPos, endPos, t);
			yield return null; 
		}
	}

    IEnumerator Rotation(Transform thisTransform, Quaternion endRotation, float time)
    {
		Quaternion startRotation = thisTransform.rotation;
		float rate = (float)(1.0/time);
		float t = 0.0f;
		while (t < 1.0) {
			t += Time.deltaTime * rate;
			thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
			yield return null;
		}
    }

	IEnumerator Rotation (Transform thisTransform, Vector3 deltaDeg, float time) {
        Quaternion endRotation = thisTransform.rotation * Quaternion.Euler(deltaDeg);
        return Rotation(thisTransform, endRotation, time);
	}
}
